<!DOCTYPE html>
<html>
<head>
  <title>Energia Reference - Tutorials </title>
  <link rel="shortcut icon" type="image/x-icon" href="http://energia.nu/img/eicon.png">
  <link rel='stylesheet' href='energiaWideRender.css' 
type='text/css' />
    <!--HeaderText--><style type='text/css'></style>  
  <meta name='robots' content='index,follow' />
  <meta name="Author" content="Robert Wessels" />
  <meta name="Publisher" content="Energia" />
  <meta name="Keywords" content="Energia, Wiring, Processing, Maker, LaunchPad, Arduino, Texas Instruments, Sketch, MSP430, mspgcc, mpsdebug, Electronic Arts, Programming, C, C++, Robert Wessels" />
  <meta name="Description" content="Energia is a rapid prototyping platform for the Texas Instruments MCU Launchpad. Energia is based on Wiring and Arduino and uses the Processing IDE." />
  <meta name="Copyright" content="All contents copyright Robert Wessels" />

</head>
<body>
<div id="page">
  <!--PageHeaderFmt-->
  <div id="pageheader">
    <div class="title"><a href='http://energia.nu'>Energia</a></div>
  </div>
  <!--/PageHeaderFmt-->

  <!--PageLeftFmt-->
  <div id="pagenav">
    <div id="navbar">
  	<p><a class='wikilink' href='http://energia.nu/'>Home</a>
<a class='wikilink' href='http://energia.nu/download'>Download</a>
<a class='wikilink' href='Guide_index.html'>Getting Started</a>
<a class='wikilink' href='index.html'>Reference</a>
<a class="wikilink" href="http://energia.nu/help.html">Getting Help</a>
<a class="nav" href="http://energia.nu/faq.html">FAQ</a>
<a class="wikilink" href="http://energia.nu/projects.html">Projects Using Energia</a>
<a class="nav" href="http://energia.nu/contact.html">Contact Us</a>
</p>


<p class='vspace'></p>

    </div>
  </div>
  <!--/PageLeftFmt-->

  <div id="pagetext">
  <!--PageText-->
<div id='wikitext'>
<p><em>Examples &gt; Basics</em>
</p>
<p class='vspace'></p><h3>Blink</h3>
<p>This example shows how to program MSP430 to blink its LED using Energia.
</p>
<h3>Hardware Required</h3>
<ul><li>MSP-EXP430G2 LaunchPad</li>
<li>LED [ available already as a part of the Launchpad]</li></ul>
<h3>Relevant groundwork</h3>
<p>MSP430 has two led pins already attached on the Launchpad. MSP430 has two I/O ports ; P0 & P1, with eight I/O lines each. The two leds are connected to port 1. The red led is connected pin 2/ P1.0 and the green led  is  connected to pin 14/p1.6.  The eight lines of  two ports can be individually  programmed with the help of dedicated port control registers. 
</p><p class='vspace'></p>
<strong><u>P1DIR:</u></strong>
<p> This control register is used in configuring the respective lines of the port as input or output. 
<p> A bit field set to 1 : configures the respective line as output
</p>
<p>Set to  0 : configures the respective line as input.
</p>
<p>Ex : The below  bit field configuration of P1DIR shows that Line 0 of port 1 is set as output.
</p>
<table border='1' cellspacing='2' cellpadding='2'>
<tr><td>7</td><td>6</td><td>5</td><td>4</td><td>3</td><td>2</td><td>1</td><td>0</td></tr>
<tr><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr>
</table><p class='vspace'></p>


<strong><u>P1OUT:</u></strong>
<p>This control register is  used to write values 0/1 in the respective bit lines configured as output. 
</p>
<p>Ex : The below  bit field configuration of P1OUT  shows that Line 0 of port 1 is set to 1.
</p>
<table border='1' cellspacing='2' cellpadding='2'>
<tr><td>7</td><td>6</td><td>5</td><td>4</td><td>3</td><td>2</td><td>1</td><td>0</td></tr>
<tr><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr>
</table>
<h3>Circuit</h3>
<p>MSP430G2 LaunchPad has  LED attached to pin 0 and pin 14 on the board itself as shown above in the image. If you run the below code  example with no hardware attached, you should see that LED blink.
</p>
<img height='250px' src='../reference/img/Blink_bb.png' alt='' title='' />
<p>image developed using <a href='http://fritzing.org'>Fritzing</a>. For more circuit examples, see the <a href='http://fritzing.org/projects'>Fritzing project page</a>.
</p>
<h3>Schematic</h3>
<img height='400px' src='../reference/img/Blink_schem.png' alt='' title='' />
<h3>Code Explanation</h3>
<p>In the program , the first thing you do is to initialize pin 2 [ RED_LED] as an output pin with the line:
</p>
<pre>
pinMode(RED_LED, OUTPUT);
</pre><p class='vspace'></p>
<p>In the main loop, you turn the LED on with the line:
</p>
<pre>
digitalWrite(RED_LED, HIGH);
</pre><p class='vspace'></p>
<p>This supplies 3 volts to pin 2. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:
</p>
<pre>
digitalWrite(RED_LINE, LOW);
</pre><p class='vspace'></p>
<p>That takes pin 2 back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the MSP430 to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time. 
</p>
<h3>Code</h3>
<pre>
/*
  Blink
  The basic Energia example.
  Turns on an LED on for one second, then off for one second, repeatedly.
  Pin 2 has an LED connected on MSP430 boards, has a name 'RED_LED' in the code.
*/
  
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(RED_LED, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(RED_LED, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(RED_LED, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

</pre>
<h3>Working Video</h3>
(Insert Video Here)
<h3>Try it out:</h3>
<p>
- Blink the green LED<br />
- Blink the red and green alternatively & together
</p>

<p class='vspace'></p><p><strong>See Also</strong>
</p><ul><li><a class='wikilink' href='Setup.html'>setup</a>()
</li><li><a class='wikilink' href='Loop.html'>loop</a>()
</li><li><a class='wikilink' href='PinMode.html'>pinMode</a>()   
</li><li><a class='wikilink' href='DigitalWrite.html'>digitalWrite</a>() 
</li><li><a class='wikilink' href='Delay.html'>delay</a>()
</li></ul>

<p class='vspace'></p><p><a class='wikilink' href='Guide_index.html'>Getting Started Home</a>
</p>
<p class='vspace'></p><p><em>Corrections, suggestions, and new documentation should be posted to the <a class='urllink' href='http://forum.43oh.com/forum/28-energia/' rel='nofollow'>Forum</a>.</em>
</p>
<p class='vspace'></p><p>The text of the Energia reference is licensed under a
<a class='urllink' href='http://creativecommons.org/licenses/by-sa/3.0/' rel='nofollow'>Creative Commons Attribution-ShareAlike 3.0 License</a>.  Energia reference is based on the Arduino reference. Code samples in the reference are released into the public domain.
</p>
</div>


  </div>

  <!--PageFooterFmt-->
  <div id="pagefooter">
&copy;Energia | 
  <a href='#'>Edit Page</a> | <a href='#'>Page History</a> | <a href=#' target='_blank'>Printable View</a> | <a href='#'>All Recent Site Changes</a>
  </div>
  <!--/PageFooterFmt-->

</div>
</body>
</html>
